{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "85f9b482",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = {}\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "13f1aaf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found ['a'] = 0\n",
    "found ['e'] = 0\n",
    "found ['i'] = 0\n",
    "found ['o'] = 0\n",
    "found ['u'] = 0\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dfcb5155",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e'] = found['e'] + 1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e8ca07f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更新频率 2.0版\n",
    "found['e'] += 1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "214b262d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "# for 循环迭代处理一个字典时，解释器只处理字典的键\n",
    "\n",
    "#要访问关联的数据值，需要把各个键放在中括号里，结合使用字典名和键来访问与这个键相关联的值\n",
    "\n",
    "for k in found:\n",
    "    print(k,'was found',found[k],'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f9188f10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in sorted(found):\n",
    "    print(k,'was found',found[k],'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f79b336a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "# 用“items”迭代处理字典,在for\n",
    "for k, v in sorted (found.items()):\n",
    "    print(k,'was found',v,'time(s).')\n",
    "# 在found字典上调用items方法\n",
    "# items传回两个循环变量\n",
    "# 在for循环中使用items通常是迭代处理字典的首选技术"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "eceff8ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:apples\n",
      "a was found 1 time(s).\n",
      "e was found 1 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a', 'e', 'i', 'o', 'u']\n",
    "word = input(\"provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "\n",
    "\n",
    "for k, v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7d521e29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits['apples'] = 10\n",
    "'apples'in fruits\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6554df39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1\n",
    "else:\n",
    "    fruits['bananas'] = 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3a85234e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 1}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'pears'not in fruits:\n",
    "    fruits['pears'] = 0  #初始化\n",
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "557c0d7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits.setdefault('pears', 0) #初始化 对比上一个初始化，这个只需要一行代码\n",
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
